import {Component} from "@angular/core";

import {ActionSheetController, AlertController, NavController, ToastController} from "ionic-angular";
import {UserData} from "../../common/user.data";
import {ImagePicker, ImagePickerOptions} from "@ionic-native/image-picker";
import {File} from "@ionic-native/file";
import {LoginPage} from "../login/login";
import {SettingPage} from "../setting/setting";
import {ArticleMyPage} from "../article/myarticle/myarticle";
import {HistoryPage} from "../history/history";
import {UserService} from "../../service/user/user.service";
import {MessagePage} from "../message/message";
import {MediaCapture} from "@ionic-native/media-capture";
import {Camera, CameraOptions} from '@ionic-native/camera';

@Component({
  selector: 'page-account',
  templateUrl: 'account.html',
  providers: [MediaCapture, Camera]
})
export class AccountPage {

  private userCenter: {
    articleCount: null
    fansCount: null
    favoriteCount: null
    messageCount: null
    topicCount: null
  };

  constructor(public actionSheetCtrl: ActionSheetController,
              public toast: ToastController,
              private file: File,
              private camera: Camera,
              private user: UserData,
              private userService: UserService,
              public nav: NavController) {
  }

  ionViewDidEnter() {
    /**
     * 初始化加载用户信息,如发表文章数等
     */
    if (!this.userCenter && this.user.token) {
      this.userService.personalCenter({token: this.user.token}).subscribe(res => {
        this.userCenter = res.data;
        console.log(res)
      });
    }
  }

  messageClick() {
    this.user.token ? this.nav.push(MessagePage) : this.nav.push(LoginPage);
  }

  settingsClick() {
    this.nav.push(SettingPage);
  }

  toLoginClick() {
    this.nav.push(LoginPage);
  }

  //我的文章
  myArticleClick() {
    this.user.token ? this.nav.push(ArticleMyPage) : this.nav.push(LoginPage);
  }

  historyClick() {
    this.nav.push(HistoryPage);
  }

  insertPicClick() {
    this.actionSheetCtrl.create({
      buttons: [
        {
          text: '从相册选择图片',
          handler: () => {
            this.picPicker();
          }
        }, {
          text: '拍照',
          handler: () => {
            this.takePicture();
          }
        }
      ]
    }).present();
  }

  private picPicker() {
    const options: CameraOptions = {
      quality: 100,
      allowEdit: true,
      targetWidth: 100,
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
      targetHeight: 100,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    };
    this.camera.getPicture(options).then((imageData) => {

      this.userService.changePhoto(this.user.token, {
        fileName: 'headPhoto.jpg',
        resource: 'data:image/jpeg;base64,' + imageData
      }).subscribe(result => {
        if (result.status === "SUCCESS") {
          this.user.user.smallHeadPic = result.data;
        } else {
          this.toast.create({
            message: result.message,
            duration: 2000,
            position: 'top'
          }).present();
        }
      });
    });
  }

  private takePicture() {
    const options: CameraOptions = {
      quality: 100,
      allowEdit: true,
      targetWidth: 100,
      sourceType: this.camera.PictureSourceType.CAMERA,
      targetHeight: 100,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    };
    this.camera.getPicture(options).then((imageData) => {

      this.userService.changePhoto(this.user.token, {
        fileName: 'headPhoto.jpg',
        resource: 'data:image/jpeg;base64,' + imageData
      }).subscribe(result => {
        if (result.status === "SUCCESS") {
          this.user.user.smallHeadPic = result.data;
        } else {
          this.toast.create({
            message: result.message,
            duration: 2000,
            position: 'top'
          }).present();
        }
      });
    });
  }
}
